package com.machine.filling.function.init.InputOilGunInfo;

import android.content.Context;

import com.google.gson.Gson;
import com.machine.filling.db.dao.GunInfoDao;
import com.machine.filling.db.modle.GunInfoModle;
import com.machine.filling.net.http.network.APIHttpManager;
import com.machine.filling.net.http.network.BaseHttpStringCallback;
import com.machine.filling.net.http.response.AddOilGunResBean;
import com.machine.filling.net.http.response.OilGunDetailResBean;
import com.machine.filling.net.http.response.OilTankListResBean;
import com.machine.filling.net.http.response.OilTypeListResBean;
import com.machine.filling.util.CustomToast;
import com.machine.filling.util.StringUtil;
import com.machine.filling.util.TimeUtils;

import okhttp3.Call;

/**
 * Created by Administrator on 2018/1/14 0014.
 */

public class InputOilGunInfoPresenter implements InputOilGunInfoContract.Presenter {
    private Context mContext;
    private InputOilGunInfoContract.View mView;


    public InputOilGunInfoPresenter(Context mContext, InputOilGunInfoContract.View mView) {
        this.mContext = mContext;
        this.mView = mView;
        start();
    }

    /**
     * 油站id
     */
    private String stationId;
    /**
     * 油枪id
     */
    private String oilGunId;
    /**
     * 油机id
     */
    private String machineId;

    @Override
    public void start() {
        stationId = mView.getStationId();
        machineId = mView.getMachineId();
        oilGunId = mView.getOilGunId();
    }

    @Override
    public void distoryContext() {


    }

    /**
     * 获取油品列表
     */
    @Override
    public void getOilTypeList() {
        mView.showProgress(null);
        APIHttpManager.getInstance().getOilTypeList(stationId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                OilTypeListResBean bean = new Gson().fromJson(result, OilTypeListResBean.class);
                if (bean.getResult() != null) {
                    mView.showOilTypeListResult(bean.getResult().getTypes());
                } else {
                    CustomToast.showToast(mContext, bean.getMsg());
                }
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext, errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 获取油罐列表
     */
    @Override
    public void getOilTankList() {
        mView.showProgress(null);
        APIHttpManager.getInstance().getOilTankList(stationId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                OilTankListResBean bean = new Gson().fromJson(result, OilTankListResBean.class);
                if (bean.getResult() != null) {
                    mView.showOilTankListResult(bean.getResult().getPots());
                } else {
                    CustomToast.showToast(mContext, bean.getMsg());
                }
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext, errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 获取油枪详情
     */
    @Override
    public void getOilGunDetailInfo() {
        if (StringUtil.isNullOrEmpty(oilGunId)) {
            return;
        }
        mView.showProgress(null);
        APIHttpManager.getInstance().getOilGunDetail(oilGunId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                OilGunDetailResBean bean = new Gson().fromJson(result, OilGunDetailResBean.class);
                if (bean.getResult() != null) {
                    mView.showOilGunDetailInfo(bean.getResult());
                } else {
                    CustomToast.showToast(mContext, bean.getMsg());
                }
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext,errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 保存油枪信息
     */
    @Override
    public void addOrModifyOilGunInfo() {
        if (StringUtil.isNullOrEmpty(oilGunId)) {
            //添加油枪
            addOilGunInfo();
        } else {
            //修改油枪
            modifyGunInfo();
        }
    }

    /**
     * 添加油枪
     */
    private void addOilGunInfo() {
        OilTypeListResBean.ResultBean.TypesBean typesBean = mView.getTypeBean();
        OilTankListResBean.ResultBean.PotsBean tankBean = mView.getPotBean();

        final String tankId = tankBean.getId();
        final String gunNum = mView.getOilGunNum();
        final String gunName = mView.getOilGunName();
        final String initAmount = mView.getInitAccount();
        final String serialNum = mView.getSerialNum();
        final String typeId = typesBean.getId();

        if (StringUtil.isNullOrEmpty(tankId) || StringUtil.isNullOrEmpty(gunNum) || StringUtil.isNullOrEmpty(gunName) ||
                StringUtil.isNullOrEmpty(initAmount) || StringUtil.isNullOrEmpty(serialNum) || StringUtil.isNullOrEmpty(tankId)) {
            CustomToast.showToast(mContext, "请完善油枪信息");
            return;
        }
        mView.showProgress(null);
        APIHttpManager.getInstance().addOilGunInfo(stationId, machineId, tankId, gunNum, gunName, initAmount, serialNum, typeId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                AddOilGunResBean bean = new Gson().fromJson(result,AddOilGunResBean.class);

                //todo 添加数据库
                GunInfoModle gunInfoModle = new GunInfoModle();
                gunInfoModle.setMachineId(bean.getResult().getGun().getMachineId());
                gunInfoModle.setStationId(bean.getResult().getGun().getSellerId());

                gunInfoModle.setGunId(bean.getResult().getGun().getId());
                gunInfoModle.setGunName(bean.getResult().getGun().getName());
                gunInfoModle.setGunNum(bean.getResult().getGun().getNum());

                gunInfoModle.setTypeId(bean.getResult().getGun().getTypeId());
                gunInfoModle.setTypeNum(mView.getTypeBean().getNum());
                gunInfoModle.setTypeShortName(mView.getTypeBean().getShortName());

                gunInfoModle.setPotId(bean.getResult().getGun().getPotId());
                gunInfoModle.setPotName(mView.getPotBean().getName());
                gunInfoModle.setPotNum(mView.getPotBean().getNum());

                gunInfoModle.setPortNum(bean.getResult().getGun().getPortNum());
                gunInfoModle.setInitAmount(initAmount);
                gunInfoModle.setCreateD(TimeUtils.getNowTime());

                GunInfoDao.getInstance().addGun(gunInfoModle);
                mView.saveOilGunInfoResult();
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext, errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }

    /**
     * 修改油枪
     */
    private void modifyGunInfo() {
        OilTypeListResBean.ResultBean.TypesBean typesBean = mView.getTypeBean();
        OilTankListResBean.ResultBean.PotsBean tankBean = mView.getPotBean();

        final String gunId = mView.getOilGunId();
        final String tankId = tankBean.getId();
        final String tankNum = tankBean.getNum();
        final String gunNum = mView.getOilGunNum();
        final String gunName = mView.getOilGunName();
        final String initAmount = mView.getInitAccount();
        final String serialNum = mView.getSerialNum();
        final String typeId = typesBean.getId();
        final String typeNum = typesBean.getNum();
        final String typeShortName = typesBean.getShortName();
        mView.showProgress(null);
        APIHttpManager.getInstance().modifyOilGunInfo(gunId, machineId, tankId, gunNum, gunName, initAmount, serialNum, typeId, new BaseHttpStringCallback() {
            @Override
            public void onSuccess(Call call, String result) {
                GunInfoModle gunInfoModle = new GunInfoModle();
                gunInfoModle.setMachineId(machineId);
                gunInfoModle.setStationId(stationId);

                gunInfoModle.setGunName(gunName);
                gunInfoModle.setGunNum(gunNum);
                gunInfoModle.setGunId(gunId);

                gunInfoModle.setTypeId(typeId);
                gunInfoModle.setTypeNum(typeNum);
                gunInfoModle.setTypeShortName(typeShortName);

                gunInfoModle.setPotId(tankId);
                gunInfoModle.setPotNum(tankNum);
                gunInfoModle.setPotId(tankId);

                gunInfoModle.setPortNum(serialNum);
                gunInfoModle.setInitAmount(initAmount);
                gunInfoModle.setCreateD(TimeUtils.getNowTime());

                GunInfoDao.getInstance().addGun(gunInfoModle);
                mView.saveOilGunInfoResult();
            }

            @Override
            public void onFailed(String errorCode, String errorMsg, Exception exception) {
                CustomToast.showToast(mContext, errorMsg);
            }

            @Override
            public void onFinished() {
                mView.hideProgress();
            }
        });
    }
}
